/***********************************************************************************************************************************
 * Name:	DB_Owner.sql
 * Author:	Frank Figearo (frank@sqlnerd.me)
 * Summary:	Change the database owner.
 */
SET IMPLICIT_TRANSACTIONS OFF; WHILE 0 < @@TRANCOUNT ROLLBACK;
GO
SELECT 'ALTER AUTHORIZATION ON DATABASE::[' + [name] + '] TO [DBOwner];'
  FROM DBAdmin.DBList('@') l INNER JOIN sys.databases d ON l.database_id = d.database_id;
GO
SELECT 'EXECUTE sp_change_users_login ''Update_One'', ''' + name + ''', ''' + name + ''''
  FROM sys.database_principals
  WHERE type = 'S' AND 4 < principal_id;
GO